<?php
/**
 * common model file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/09/03
 * Time: 15:20
 */

namespace HiCommon\Model;


class Shop extends Base
{

    protected $table = 'shop';

    protected $guarded = [];

    //public $timestamps = false;

    /**
     * 应该被调整为日期的属性
     *
     * @var array
     */
    protected $dates = [
    ];

    //省
    public function province_name()
    {
        return $this->hasOne(Areas::class, 'id', 'province')
            ->select('id', 'name');
    }

    //市
    public function city_name()
    {
        return $this->hasOne(Areas::class, 'id', 'city')
            ->select('id', 'name', 'Lng', 'Lat');
    }

    //区
    public function area_name()
    {
        return $this->hasOne(Areas::class, 'id', 'area')
            ->select('id', 'name', 'mername', 'Lng', 'Lat');
    }

    public function road_rescue()
    {
        return $this->hasMany(RoadRescueConfig::class, 'repairstore_id');
    }


    //一对多关联检测机构服务类型
    public function checkpoint_service_type()
    {
        return $this->hasMany(CheckpointServiceType::class, 'checkpoint_id', 'id')
            ->select('id', 'checkpoint_id', 'title');
    }

    //一对多关联开放日期
    public function appointment_day()
    {
        $take_num = intval(common_config()['api_take_num']);
        return $this->hasMany(CheckpointMakeDate::class, 'checkpoint_id', 'id')
            ->where('day', '>=', date('Y-m-d', time()))
            ->orderBy('day', 'asc')
            ->take($take_num)
            ->select('id', 'checkpoint_id', 'day', 'am_time', 'am_num', 'am_order', 'pm_time', 'pm_num', 'pm_order');
    }

    //一对多关联车检费用
    public function year_check_fee()
    {
        return $this->hasMany(CheckpointOutlay::class, 'checkpoint_id', 'id')
            ->select('id', 'checkpoint_id', 'title', 'pic_url');
    }

    //一对多关联车检费用
    public function shop_img()
    {
        return $this->hasMany(ShopImg::class, 'shop_id')->select('id', 'shop_id', 'img');
    }

    //一对一关联分类
    public function type()
    {
        return $this->hasOne(CheckpointType::class, 'id', 'type')
            ->select('id', 'title');
    }

    //一对多关联检测机构服务类型
    public function service_type()
    {
        return $this->hasMany(CheckpointServiceType::class, 'checkpoint_id', 'id')
            ->select('id', 'checkpoint_id', 'title', 'price');
    }

    //一对多关联开放日期
    public function make_day()
    {
        return $this->hasMany(CheckpointMakeDate::class, 'checkpoint_id', 'id')
            ->where('day', '>=', date('Y-m-d', time()))
            ->select('id', 'checkpoint_id', 'day', 'am_time', 'am_num', 'am_order', 'pm_time', 'pm_num', 'pm_order');
    }

    //一对多关联车检费用
    public function outlay()
    {
        return $this->hasMany(CheckpointOutlay::class, 'checkpoint_id', 'id')
            ->select('id', 'checkpoint_id', 'title', 'pic_url');
    }

    //一对多关联门店技师
    public function master()
    {
        return $this->hasMany(ShopMaster::class, 'shop_id', 'id')
            ->select('id', 'shop_id', 'img', 'name', 'phone');
    }

    //关联商品订单表统计该门店的订单数量
    public function store_order_num()
    {
        return $this->hasMany(StoreOrder::class, 'shop_id', 'id')
            ->select('id', 'shop_id');
    }

    //关联服务订单表统计该门店的订单数量
    public function repair_order_num()
    {
        return $this->hasMany(OrderRepairstore::class, 'shop_id', 'id')
            ->select('id', 'shop_id');
    }

    //一对多关联店铺用户
    public function shop_user()
    {
        return $this->hasMany(ShopUser::class, 'shop_id')->select('id', 'shop_id');
    }

    public function shop_master_user()
    {
        return $this->hasOne(ShopUser::class, 'shop_id')->where('is_master', 1);
    }


    //一对一关联商户资金账户信息
    public function shop_account()
    {
        return $this->hasOne(ShopAccount::class, 'id');
    }


    //一对一关联商户指定账户是否已经关注
    public function is_collect()
    {
        return $this->hasMany(Collect::class, 'x_id', 'id')->where('type', 4);
    }

    //门店关联收藏
    public function collect()
    {
        return $this->hasMany(Collect::class, 'x_id', 'id');
    }

    //店铺评分
    public function shop_score()
    {
        return $this->hasOne(ViewShopAppriseScore::class, 'id')->select(['id', 'average_score']);
    }

    /**
     * 获取距离转换值
     *
     * @param $value
     *
     * @return string
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getDistanceAttribute($value)
    {
        if ($value < 1) {
            $value = number_format($value, 3) * 1000 . 'm';
        } else {
            $value = number_format($value, 2) . 'km';
        }
        return $value;
    }

    /**
     * 获取状态对应的名称
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getStateAttribute($value)
    {
        switch ($value) {
            case 0:
                $res = ['id' => 0, 'name' => '草稿'];
                break;
            case 1:
                $res = ['id' => 1, 'name' => '待审核'];
                break;
            case 2:
                $res = ['id' => 2, 'name' => '通过'];
                break;
            case 3:
                $res = ['id' => 3, 'name' => '拒绝'];
                break;
            default:
                $res = ['id' => -1, 'name' => '未知'];
        }
        return $res;
    }

    /**
     * 获取图片可用链接
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getImgUrlAttribute($value)
    {
        $arr_data = [
            'src' => $value,
            'full_src' => oss_full_url($value),
        ];
        return $arr_data;
    }

    /**
     * 获取图片可用链接
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getImgAttribute($value)
    {
        $arr_data = [
            'src' => $value,
            'full_src' => oss_full_url($value),
        ];
        return $arr_data;
    }


    /**
     * 获取图片可用链接
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getLogoAttribute($value)
    {
        $arr_data = [
            'src' => $value,
            'full_src' => oss_full_url($value),
        ];
        return $arr_data;
    }

    /**
     * 扩展信息
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getExtendAttribute()
    {
        $n_status = $this->status;
        if (is_null($n_status)) {
            $n_status = -1;
        }
        switch ($n_status) {
            case 0:
                $arr_status = ['id' => $n_status, 'name' => '审核中'];
                break;
            case 1:
                $arr_status = ['id' => $n_status, 'name' => '已通过'];
                break;
            case 2:
                $arr_status = ['id' => $n_status, 'name' => '已禁用'];
                break;
            case 3:
                $arr_status = ['id' => $n_status, 'name' => '已失败'];
                break;
            default:
                $arr_status = ['id' => -1, 'name' => '缺少status字段'];
        }
        $arr_data = [];
        if (!is_null($n_status)) {
            $arr_data['status'] = $arr_status;
        }
        return $arr_data;
    }


}